package com.zone.domain.credit.service.quote;

import com.alibaba.fastjson2.JSON;
import com.zone.domain.credit.event.UserCreditAdjustEvent;
import com.zone.domain.credit.model.aggregate.CreditAdjustAggregate;
import com.zone.domain.credit.model.entity.UserCreditAccountEntity;
import com.zone.domain.credit.model.entity.UserCreditAdjustAmountEntity;
import com.zone.domain.credit.model.entity.UserCreditOrderEntity;
import com.zone.domain.credit.repository.ICreditRepository;
import com.zone.domain.credit.service.IUserCreditAccountQuoteService;
import com.zone.domain.task.model.entity.TaskEntity;
import com.zone.domain.task.model.valobj.TaskState;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.Validate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * @author: zongzi
 * @description: 用户积分账户额度变更相关操作实现类
 * @date: 2025/3/11
 */
@Slf4j
@Service
public class UserCreditAccountQuoteService implements IUserCreditAccountQuoteService {
    @Autowired
    private ICreditRepository creditRepository;
    /** 消息主题 */
    @Value("${spring.rabbitmq.topic.user_credit_adjust}")
    private String userCreditAdjustTopic;

    @Override
    public void adjustAmount(UserCreditAdjustAmountEntity adjustAmountEntity) {
        // 数据校验
        Validate.notEmpty(adjustAmountEntity.getUserId(), "用户id不能为空");
        Validate.notEmpty(adjustAmountEntity.getTradeName(), "积分交易名称不能为空");
        Validate.notNull(adjustAmountEntity.getTradeTypeEnum(), "积分交易类型不能为空");
        Validate.notNull(adjustAmountEntity.getAmount(), "积分交易金额不能为空");
        // 构造聚合对象
        CreditAdjustAggregate creditAdjustAggregate = new CreditAdjustAggregate();
        creditAdjustAggregate.setUserId(adjustAmountEntity.getUserId());
        creditAdjustAggregate.setAmount(adjustAmountEntity.getAmount());
        creditAdjustAggregate.setTradeTypeEnum(adjustAmountEntity.getTradeTypeEnum());
        // 用户积分流水单
        UserCreditOrderEntity userCreditOrderEntity = UserCreditOrderEntity.builder()
                .userId(adjustAmountEntity.getUserId())
                .tradeAmount(adjustAmountEntity.getAmount())
                .tradeType(adjustAmountEntity.getTradeTypeEnum().getCode())
                .tradeName(adjustAmountEntity.getTradeName())
                .orderId(RandomStringUtils.randomNumeric(12))
                .outBusinessNo(adjustAmountEntity.getOutBusinessNo())
                .build();
        creditAdjustAggregate.setUserCreditOrderEntity(userCreditOrderEntity);
        // 任务
        UserCreditAdjustEvent userCreditAdjustEvent = new UserCreditAdjustEvent();
        userCreditAdjustEvent.setUserId(adjustAmountEntity.getUserId());
        userCreditAdjustEvent.setTradeName(adjustAmountEntity.getTradeName());
        userCreditAdjustEvent.setTradeTypeEnum(adjustAmountEntity.getTradeTypeEnum());
        userCreditAdjustEvent.setAmount(adjustAmountEntity.getAmount());
        userCreditAdjustEvent.setOutBusinessNo(adjustAmountEntity.getOutBusinessNo());

        TaskEntity taskEntity = new TaskEntity();
        taskEntity.setUserId(adjustAmountEntity.getUserId());
        taskEntity.setTopic(userCreditAdjustTopic);
        taskEntity.setMessageId(RandomStringUtils.randomNumeric(11));
        taskEntity.setMessage(JSON.toJSONString(userCreditAdjustEvent));
        taskEntity.setState(TaskState.CREATE);
        creditAdjustAggregate.setTaskEntity(taskEntity);

        // 数据落库
        creditRepository.adjustAccountAmount(creditAdjustAggregate);
    }

    @Override
    public UserCreditAccountEntity queryCreditAccount(String userId) {
        return creditRepository.queryCreditAccount(userId);
    }
}
